package ptij.vocoder.baseModel;

import java.io.*;

import ptolemy.actor.*;
import ptolemy.kernel.*;
import ptolemy.kernel.util.*;
import ptolemy.data.ObjectToken;
import ptolemy.data.StringToken;
import ptolemy.data.type.*;
import ptolemy.data.expr.Parameter;

import ptij.vocoder.baseModel.*;

//////////////////////////////////////////////////////////////////////////
//// FrameWriter
/** 
@author Ivan Jeukens
@version v 0.1 07/08/2003
*/
public class FrameWriter extends TypedAtomicActor {

    public FrameWriter(CompositeEntity container, String name) throws
            IllegalActionException, NameDuplicationException {
        super(container, name);

        input = new TypedIOPort(this, "input", true, false);
        input.setTypeEquals(BaseType.OBJECT);

        fileName = new Parameter(this, "fileName",
             new StringToken("spch.out"));
    }

    ///////////////////////////////////////////////////////////////////
    ////                     ports and parameters                  ////

    // Input ports

    // Output Ports
    public TypedIOPort input;

    public Parameter fileName;
    
    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    public void initialize() throws IllegalActionException {
        super.initialize();

        StringToken st = (StringToken) fileName.getToken();
        String fname = (String) st.stringValue();

        try {
            File f = new File(fname);
            FileOutputStream fo = new FileOutputStream(f);
            _dataOut = new DataOutputStream(fo);
        }
        catch(IOException ex) {
            System.out.println(ex);
            _dataOut = null;
        }
    }

    public void fire() throws IllegalActionException {
        ObjectToken obj = (ObjectToken) input.get(0);
        Short [] frame = (Short []) obj.getValue();

        for(int i = 0;i < Cnst.L_FRAME;i++) {
            try {
                _dataOut.writeShort(frame[i].shortValue());
            }
            catch(IOException ex) {
                System.out.println(ex);
            }
        }
    }

    ///////////////////////////////////////////////////////////////////
    ////                         private methods                   ////

    ///////////////////////////////////////////////////////////////////
    ////                         private variables                 ////

    private DataOutputStream _dataOut;    
}
